var qqmapsdk, app = getApp(), QQMapWX = require("../utils/qqmap-wx-jssdk.js");

Page({
    data: {
        userInfo: {},
        hasUserInfo: !1,
        canIUse: wx.canIUse("button.open-type.getUserInfo"),
        swiperCurrent: 0,
        indicatorDots: !1,
        autoplay: !0,
        interval: 5e3,
        duration: 1e3,
        bomb: !1,
        page: 1,
        Speed: 50,
        activity_list: [],
        ii:0
    },
    onLoad: function(o) {   
      var self = this;    
      wx.checkSession({
        success() {
          // app.getUserInfoZ(function (o) {
          //   console.log('get userinfo')
          // })
          // app.toLoginPage();
        },
        fail() {
          wx.navigateTo({
            url: "../login/index"
          })
        }
      })
      wx.getSetting({
        success(res) {
          if (res.authSetting['scope.userLocation']) {
            
          } else {
            console.log('no location')
            self.setData({
              page: 1,
              activity_list: []
            }), self.advert(), wx.stopPullDownRefresh();
          }
          // res.authSetting = {
          //   "scope.userInfo": true,
          //   "scope.userLocation": true
          // }
        }
      })
        console.log('================indexOnLoad==========')
        var i = this;
      
        app.getUserInfo(function(t) {
            console.log(t);
            console.log('0--------------++++++++')  
            
            var a = wx.getStorageSync("location");
          console.log('1--------------++++++++') 
         
            a.latitude, a.longitude;
          console.log('2--------------++++++++') 
          console.log('================i.citys==========')
          i.citys();
          //app.getSystem(this, { success: i.citys()})
            
          console.log('3--------------++++++++') 
           
            var e = decodeURIComponent(o.scene);
            if (null != e) var n = e;
            if (null != o.upper_partner) n = o.upper_partner;
            i.setData({
                partner_lower: n,
                userInfo: t
            }), i.fenxiao();
        }), i.setData({
            today: app.util.time()
        }), app.util.request({
            url: "entry/wxapp/typeList",
            cachetime: "0",
            success: function(t) {
                console.log("这是分类"), console.log(t), i.setData({
                    typeList: t.data
                });
            }
        });

      
    },
    fenxiao: function(t) {
        var a = this.data.userInfo, e = a.id, n = this.data.partner_lower;
        app.util.request({
            url: "entry/wxapp/MySx",
            data: {
                user_id: e
            },
            success: function(t) {
               console.log(t.data,'+++++++++++++++++++++++++++')
                console.log(t), 0 == t.data && "undefined" != n && app.util.request({
                    url: "entry/wxapp/Binding",
                    cachetime: "0",
                    data: {
                        fx_user: a.id,
                        user_id: n
                    },
                    success: function(t) {
                        console.log("这是成为下级分销商" + n), console.log(t);
                    }
                });
            }
        });
    },
    citys: function(t) {
        var n = this;
        n.data.ii++
        console.log('4--------------++++++++', n.data.ii, n.data.system)
        if (n.data.ii>10){return;}
        if (null == n.data.system) n.citys(); else {
            console.log('6--------------++++++++') 
            wx.setStorageSync("platform", n.data.system), 
            wx.setNavigationBarTitle({
                title: n.data.system.pt_name
            });
            var o = n.data.system.map_key;
          console.log('7--------------++++++++') 
            1 == n.data.system.city_open ? 1 == app.globalData.sele_city ? wx.getLocation({
                type: "wgs84",
                success: function(t) {
                    wx.setStorageSync("Location", t);
                    var a = t.latitude, e = t.longitude;
                    new QQMapWX({
                        key: o
                    }).reverseGeocoder({
                        location: {
                            latitude: a,
                            longitude: e
                        },
                        success: function(t) {
                          console.log('8--------------++++++++') 
                            console.log(t);
                            var a = t.result.ad_info.city, e = [ t.result.ad_info.province, t.result.ad_info.city, t.result.ad_info.district ];
                          console.log(a), wx.setStorageSync("location_city", e), wx.setStorageSync("city", a), 
                            app.util.request({
                                url: "entry/wxapp/SaveCity",
                                cachetime: "0",
                                data: {
                                    cityname: a
                                },
                                success: function(t) {
                                  console.log('9--------------++++++++') 
                                    console.log("这是保存城市");
                                }
                            }), n.setData({
                                city: a,
                                Speed: 70
                            }), n.advert();
                        },
                        fail: function(t) {
                            console.log(t);
                        },
                        complete: function(t) {
                            console.log(t), "request:fail url not in domain list" == t.message && wx.showModal({
                                title: "",
                                content: "检测到没有配置map域名"
                            });
                        }
                    });
                },
                fail: function(t) {
                    wx.getSetting({
                        success: function(t) {
                            0 == t.authSetting["scope.userLocation"] && wx.openSetting({
                                success: function(t) {}
                            });
                        }
                    });
                }
            }) : (console.log("检测到不是空的"), n.setData({
                city: wx.getStorageSync("city"),
                Speed: 70
            }), n.advert()) : (wx.setStorageSync("city", ""), n.setData({
                city: "",
                Speed: 70
            }), n.advert());
        }
        console.log('5--------------++++++++') 
    },
    berak: function(t) {
        this.activity(), this.setData({  
          city: wx.getStorageSync("city")
        });
    },
    advert: function(t) {
        var a = this;
        if ("" == wx.getStorageSync("city") || null == wx.getStorageSync("city")) var e = a.data.city; else e = wx.getStorageSync("city");
        app.util.request({
            url: "entry/wxapp/ad",
            cachetime: "0",
            data: {
                type: 1,
                cityname: e
            },
            success: function(t) {
                console.log("这是首页轮播图"), a.setData({
                    home_carousel_figure: t.data
                });
            }
        }), app.util.request({
            url: "entry/wxapp/ad",
            cachetime: "0",
            data: {
                type: 2,
                cityname: e
            },
            success: function(t) {
                console.log("这是专题精选"), console.log(t), a.setData({
                    Thematic_selection: t.data
                });
            }
        }), a.setData({
            Speed: 80
        }), a.refresh();
    },
    refresh: function(t) {
        var o = this, a = wx.getStorageSync("city");
        o.setData({
            city: a
        }), o.db_tab(), app.util.request({
            url: "entry/wxapp/GroupGoods",
            cachetime: "0",
            data: {
                type_id: "",
                page: 1
            },
            success: function(t) {
                console.log("商品列表", t), o.setData({
                    group_list: t.data
                });
            }
        }), app.util.request({
            url: "entry/wxapp/getnav",
            cachetime: "0",
            success: function(t) {
                console.log("这是首页导航"), console.log(t), t.data.length <= 5 ? o.setData({
                    height: 150
                }) : 5 < t.data.length && o.setData({
                    height: 300
                });
                for (var a = [], e = 0, n = t.data.length; e < n; e += 10) a.push(t.data.slice(e, e + 10));
                console.log(a), o.setData({
                    nav: a,
                    navs: t.data
                }), o.setData({
                    Speed: 90
                }), o.activity();
            }
        });
    },
    activity: function(t) {
        var n = this;
        console.log(n.data);
        var o = n.data.typeList, i = n.data.today, s = n.data.page;
        console.log("当前的页数为" + s);
        var c = n.data.activity_list, r = [];
        if ("" == wx.getStorageSync("city") || null == wx.getStorageSync("city")) var a = n.data.city; else a = wx.getStorageSync("city");
        console.log("当前选择的城市为" + a);
        i = app.today_time();
        app.util.request({
            url: "entry/wxapp/ActivityList",
            cachetime: "0",
            data: {
                page: s,
                cityname: a
            },
            success: function(t) {
                if (console.log("这是活动列表"), console.log(t), n.setData({
                    Speed: 100
                }), 0 < t.data.length) {
                    for (var a in c = c.concat(t.data), r = t.data, r) {
                        for (var e in r[a].end_time = app.ormatDate(r[a].end_time), r[a].start_time = app.ormatDate(r[a].start_time), 
                        1 == r[a].is_close && i < r[a].end_time ? i >= r[a].start_time ? r[a].activity_over = 3 : r[a].activity_over = 1 : r[a].activity_over = 2, 
                        o) r[a].type_id == o[e].id && (r[a].type_id = o[e].type_name);
                        0 == r[a].cost ? r[a].cost = "免费" : r[a].cost = "￥" + r[a].cost, r[a].start_time = r[a].start_time.slice(0, 16), 
                        r[a].yestoday = app.yestoday(r[a].start_time);
                    }
                    console.log(r), n.setData({
                        activity_list: c,
                        page: s + 1
                    });
                }
            }
        });
    },
    db_tab: function(t) {
        var n = this, o = [ {
            icon1: "../img/shouye@2x.png",
            icon2: "../img/shouyee@2x.png",
            name: "首页",
            src: "../index/index",
            img: "../img/shouyee@2x.png"
        }, {
            icon1: "../img/fennei2x.png",
            icon2: "../img/fenlei@2x.png",
            name: "分类",
            src: "../classification/classification",
            img: "../img/fenlei@2x.png"
        }, {
            icon1: "../img/huodong@2x.png",
            icon2: "../img/huodongz@2x.png",
            name: "我的票券",
            src: "../piaoquan/piaoquan",
            img: "../img/huodongz@2x.png"
        }, {
            icon1: "../img/geren@2x.png",
            icon2: "../img/gerenq@2x.png",
            name: "我的",
            src: "../logs/logs",
            img: "../img/gerenq@2x.png"
        }, {
            img: "../img/jia@2x.png"
        } ];
        console.log(o);
        var i = wx.getStorageSync("url");
        console.log(i), app.util.request({
            url: "entry/wxapp/DbMenu",
            cachetime: "0",
            success: function(t) {
                for (var a in console.log("这是底部菜单栏"), console.log(t), t.data) t.data[a].icon1 = i + t.data[a].icon1, 
                t.data[a].icon2 = i + t.data[a].icon2;
                1 == t.data.length ? (o[1].icon1 = t.data[0].icon1, o[1].icon2 = t.data[0].icon2, 
                o[1].src = t.data[0].src, o[1].name = t.data[0].name, o[1].img = t.data[0].icon2) : 2 <= t.data.length && (o[1].icon1 = t.data[0].icon1, 
                o[1].icon2 = t.data[0].icon2, o[1].src = t.data[0].src, o[1].name = t.data[0].name, 
                o[1].img = t.data[0].icon2, o[2].icon1 = t.data[1].icon1, o[2].icon2 = t.data[1].icon2, 
                o[2].src = t.data[1].src, o[2].name = t.data[1].name, o[2].img = t.data[1].icon2), 
                wx.setStorageSync("db_tab", o);
                var e = app.contains(o, "../index/index");
                o[e].img = o[e].icon1, n.setData({
                    db_tab: o
                });
            }
        });
    },
    search: function(t) {
        console.log(t);
        wx.navigateTo({
            url: "search"
        });
    },
    skip: function(t) {
        console.log(t);
        var a = t.currentTarget.dataset.index, e = this.data.navs[a];
        if (console.log(e), "" != e.src) {
            var n = e.src.replace(/(\d+|\s+)/g, ""), o = e.src.replace(/[^0-9]/gi, "");
            console.log(n + o), console.log(o), wx.navigateTo({
                url: String(n) + String(o)
            });
        } else "" != e.appid ? wx.navigateToMiniProgram({
            appId: e.appid,
            success: function(t) {
                console.log(t);
            }
        }) : "" != e.wb_src && wx.navigateTo({
            url: "link?link=" + e.wb_src
        });
    },
    myyanpiao: function() {
        wx.scanCode({
            success: function(t) {
                var a = t.path.slice(42);
                console.log(a), wx.navigateTo({
                    url: "../logs/inspect_ticket?arr=" + a
                });
            }
        });
    },
    group: function(t) {
        wx.navigateTo({
            url: "../collage/info?id=" + t.currentTarget.dataset.id
        });
    },
    swiper_skip: function(t) {
        var a = t.currentTarget.dataset.index, e = this.data.home_carousel_figure[a];
        if (console.log(e), "" != e.src) {
            var n = e.src.replace(/(\d+|\s+)/g, ""), o = e.src.replace(/[^0-9]/gi, "");
            console.log(n + o), console.log(o), wx.navigateTo({
                url: String(n) + String(o)
            });
        } else "" != e.appid ? wx.navigateToMiniProgram({
            appId: e.appid,
            success: function(t) {
                console.log(t);
            }
        }) : "" != e.wb_src && wx.navigateTo({
            url: "link?link=" + e.wb_src
        });
    },
    selection_skip: function(t) {
        var a = t.currentTarget.dataset.index, e = this.data.Thematic_selection[a];
        if (console.log(e), "" != e.src) {
            var n = e.src.replace(/(\d+|\s+)/g, ""), o = e.src.replace(/[^0-9]/gi, "");
            console.log(n + o), console.log(o), wx.navigateTo({
                url: String(n) + String(o)
            });
        } else "" != e.appid ? wx.navigateToMiniProgram({
            appId: e.appid,
            success: function(t) {
                console.log(t);
            }
        }) : "" != e.wb_src && wx.navigateTo({
            url: "link?link=" + e.wb_src
        });
    },
    bindViewTap: function() {
        wx.navigateTo({
            url: "../logs/logs"
        });
    },
    info: function(t) {
        console.log(t);
        var a = t.currentTarget.id, e = t.currentTarget.dataset.type_name;
        wx.navigateTo({
            url: "../activeinfo/activeinfo?id=" + a + "&type_name=" + e
        });
    },
    guanbi: function(t) {
        this.setData({
            bomb: !0
        });
    },
    wode: function() {
        var t = this.data.db_tab;
        wx.reLaunch({
            url: t[3].src
        });
    },
    fabu: function() {
        wx.reLaunch({
            url: "../fabu/fabu"
        });
    },
    index: function() {
        var t = this.data.db_tab;
        wx.reLaunch({
            url: t[0].src
        });
    },
    classifination: function(t) {
        var a = this.data.db_tab;
        wx.reLaunch({
            url: a[1].src
        });
    },
    mine_activity: function(t) {
        var a = this.data.db_tab;
        wx.reLaunch({
            url: a[2].src
        });
    },
    swiperChange: function(t) {
        this.setData({
            swiperCurrent: t.detail.current
        });
    },
    address: function(t) {
        var a = this.data.system.city_open;
        console.log(a), 1 == a && (this.setData({
            page: 1,
            activity_list: []
        }), wx.redirectTo({
            url: "../city/city"
        }));
    },
    getUserInfo: function(t) {
        app.globalData.userInfo = t.detail.userInfo, this.setData({
            userInfo: t.detail.userInfo,
            hasUserInfo: !0
        });
    },
    bindgetuserinfo: function(t) {
        console.log(t);
        var a = this;
        "getUserInfo:fail auth deny" == t.detail.errMsg ? (console.log("用户拒绝授权"), app.getUserInfo(function(t) {
            a.setData({
                userInfo: t,
                bind_user: !1
            });
        })) : (console.log("用户允许授权"), app.getUserInfo(function(t) {
            a.setData({
                userInfo: t,
                bind_user: !0
            });
        }));
    },
    onReady: function() {
        this.setData({
            xuanran: !1
        });
    },
    onShow: function() {
        console.log('================indexOnshow==========')
        app.setNavigationBarColor(this)
        app.getSystem(this) 
        app.getUrl(this),
        this.setData({
            xuanran: !0
        });
    },
    onPullDownRefresh: function() {
        this.setData({
            page: 1,
            activity_list: []
        }), this.advert(), wx.stopPullDownRefresh();
    },
    onHide: function() {},
    onUnload: function() {},
    onReachBottom: function() {
        console.log("上拉触底"), this.activity(), console.log(this.data.page);
    },
    onShareAppMessage: function() {}
});